package com.mingqijia.gassafety.authority.security.facade;

import com.mingqijia.gassafety.authority.api.resp.manager.UserProfileRespDTO;
import com.mingqijia.gassafety.authority.api.resp.resource.ResourceLiteRespDTO;
import com.mingqijia.gassafety.authority.api.web.facade.AuthorityManageFacade;
import com.mingqijia.gassafety.authority.dal.handler.CompanyTranslator;
import com.mingqijia.gassafety.authority.domain.adapter.BenAdapter;
import com.mingqijia.gassafety.authority.security.provider.AuthorityProvider;
import com.mingqijia.gassafety.authority.security.user.SecurityUserLoginHolder;
import com.mingqijia.gassafety.authority.security.user.SecurityUserTranslator;
import com.mingqijia.gassafety.authority.security.valueobject.ManagerVO;
import com.mingqijia.gassafety.db.entity.auth.CompanyEntry;
import com.mingqijia.gassafety.shared.constant.Constants;
import com.mingqijia.gassafety.shared.enums.ManagerType;
import com.mingqijia.gassafety.shared.facade.BaseFacade;
import com.mingqijia.gassafety.shared.handler.GlobalAuthenticationException;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * 授权管理
 * @author Damon S.
 * @version v0.2
 * @date {YEAR}年{MONTH}月{DAY}日 {TIME}
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class AuthorityManageFacadeImpl extends BaseFacade implements AuthorityManageFacade {

    private final AuthorityProvider provider;
    private final SecurityUserLoginHolder holder;
    private final SecurityUserTranslator translator2;
    private final CompanyTranslator cTranslator;

    private final BenAdapter benAdapter;

    @Override
    public String genCaptcha(String username, HttpServletResponse response) {
        return provider.createCaptcha(username, response);
    }

    @Override
    public String refreshBenToken() {
        return benAdapter.refreshBenToken(ApplicationUtils.getWorkingUser().getUsername());
    }

    @Override
    public UserProfileRespDTO getUserProfile(HttpServletRequest request) {
        ManagerVO backend = (ManagerVO) getUserBackend();
        if (CollectionUtils.isEmpty(backend.getRoleIds())) {
            throw new GlobalAuthenticationException("用户未设置角色");
        }
        UserProfileRespDTO profile = translator2.toProfileRespDTO(backend);
        profile.setBenToken(benAdapter.getBenToken());

        List<CompanyEntry> companies = provider.getAuthorizedSp();
        profile.setAuthorizedSp(cTranslator.toLiteRespDTO(companies));

        profile.getAuthorizedSp().forEach(item -> {
            item.setOrgType(ManagerType.ENTERPRISE.getCode());
            if (Objects.equals(item.getId(), Constants.PLATFORM_SP)) {
                item.setOrgType(ManagerType.PLATFORM.getCode());
            }
        });
        return profile;
    }

    @Override
    public List<ResourceLiteRespDTO> getAuthorizedRes(String workingSpId) {
        // 更新 spId 的操作
        holder.whenWorkingSpIdChanged(workingSpId);
        // 获取用户权限
        return provider.getAuthorizedRes(workingSpId);
    }
}
